/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32g0xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32g0xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "sys_timeSlice.h"
#include "dri_hmi.h"
#include "bsp_usart.h"
#include "dri_slaver.h"
#include "dri_master.h"
#include "dri_DigitalQuantity.h"
#include "dri_Pulse.h"
#include "dri_AnalogQuantity.h"
#include "sys_data.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_adc1;
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M0+ Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVC_IRQn 0 */

  /* USER CODE END SVC_IRQn 0 */
  /* USER CODE BEGIN SVC_IRQn 1 */

  /* USER CODE END SVC_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */
	gTickTime++;
	System_TaskUpdate();
  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32G0xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32g0xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles EXTI line 4 to 15 interrupts.
  */
void EXTI4_15_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI4_15_IRQn 0 */

  /* USER CODE END EXTI4_15_IRQn 0 */
  if (LL_EXTI_IsActiveRisingFlag_0_31(LL_EXTI_LINE_5) != RESET)
  {
    LL_EXTI_ClearRisingFlag_0_31(LL_EXTI_LINE_5);
    /* USER CODE BEGIN LL_EXTI_LINE_5_RISING */
    DRI_PulseIncrement_callback();
    /* USER CODE END LL_EXTI_LINE_5_RISING */
  }
  /* USER CODE BEGIN EXTI4_15_IRQn 1 */

  /* USER CODE END EXTI4_15_IRQn 1 */
}

/**
  * @brief This function handles DMA1 channel 1 interrupt.
  */
void DMA1_Channel1_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel1_IRQn 0 */

  /* USER CODE END DMA1_Channel1_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_adc1);
  /* USER CODE BEGIN DMA1_Channel1_IRQn 1 */

  /* USER CODE END DMA1_Channel1_IRQn 1 */
}

/**
  * @brief This function handles DMA1 channel 2 and channel 3 interrupts.
  */
void DMA1_Channel2_3_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel2_3_IRQn 0 */
  if(LL_DMA_IsActiveFlag_TC2(DMA1))
  {
	  LL_DMA_ClearFlag_TC2(DMA1);
	  LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_2);  //关闭DMA
	  LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_2,RS232_SLAVER_DMARX_NUM);
	  LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_2);
  }
  /* USER CODE END DMA1_Channel2_3_IRQn 0 */

  /* USER CODE BEGIN DMA1_Channel2_3_IRQn 1 */
  if(LL_DMA_IsActiveFlag_TC3(DMA1))
  {
	  LL_DMA_ClearFlag_TC3(DMA1);
	  LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_3);  //关闭DMA
	  LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_3,RS485_MASTER_DMARX_NUM);
	  LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_3);
  }
  /* USER CODE END DMA1_Channel2_3_IRQn 1 */
}

/**
  * @brief This function handles DMA1 channel 4, channel 5, channel 6, channel 7 and DMAMUX1 interrupts.
  */
void DMA1_Ch4_7_DMAMUX1_OVR_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Ch4_7_DMAMUX1_OVR_IRQn 0 */
  if(LL_DMA_IsActiveFlag_TC4(DMA1))
  {
	  LL_DMA_ClearFlag_TC4(DMA1);
	  LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_4);  //关闭DMA
	  LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_4,RS485_SLAVER_DMARX_NUM);
	  LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_4);
  }
  /* USER CODE END DMA1_Ch4_7_DMAMUX1_OVR_IRQn 0 */

  /* USER CODE BEGIN DMA1_Ch4_7_DMAMUX1_OVR_IRQn 1 */

  /* USER CODE END DMA1_Ch4_7_DMAMUX1_OVR_IRQn 1 */
}

/**
  * @brief This function handles TIM3 global interrupt.
  */
void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */
	if(LL_TIM_IsActiveFlag_UPDATE(TIM3) == SET)
	{
		LL_TIM_ClearFlag_UPDATE(TIM3);

		DRI_updataPulsePerCycle_callback();
	}
  /* USER CODE END TIM3_IRQn 0 */
  /* USER CODE BEGIN TIM3_IRQn 1 */

  /* USER CODE END TIM3_IRQn 1 */
}

/**
  * @brief This function handles SPI2 global interrupt.
  */
void SPI2_IRQHandler(void)
{
  /* USER CODE BEGIN SPI2_IRQn 0 */

  /* USER CODE END SPI2_IRQn 0 */
  /* USER CODE BEGIN SPI2_IRQn 1 */

  /* USER CODE END SPI2_IRQn 1 */
}

/**
  * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
  /* USER CODE END USART1_IRQn 0 */
  /* USER CODE BEGIN USART1_IRQn 1 */
	if(LL_USART_IsActiveFlag_IDLE(USART1))  //空闲
	{
		LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_2);  //关闭DMA

		gBus232_HMI_RxCounter = RS232_SLAVER_DMARX_NUM - LL_DMA_GetDataLength(DMA1, LL_DMA_CHANNEL_2);  //获取接收数量

		for(uint16_t i = 0; i < gBus232_HMI_RxCounter; i++)
		{
			gBus232_HMI_RxBuffer[i] = gUSART1_DMARx_HMI_buffer[i];
		}

		//重新配置DMA
		LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_2, RS232_SLAVER_DMARX_NUM);
		LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2);
	}

	LL_USART_ClearFlag_IDLE(USART1);  //清除中断标志位

  /* USER CODE END USART1_IRQn 1 */
}

/**
  * @brief This function handles USART2 global interrupt / USART2 wake-up interrupt through EXTI line 26.
  */
void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
  /* USER CODE END USART2_IRQn 0 */
  /* USER CODE BEGIN USART2_IRQn 1 */
	if(LL_USART_IsActiveFlag_IDLE(USART2))  //空闲
	{
		LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_3);  //关闭DMA

		gBus485_Master_RxCounter = RS485_MASTER_DMARX_NUM-LL_DMA_GetDataLength(DMA1,LL_DMA_CHANNEL_3);  //获取接收数量

		for(uint16_t i = 0; i < gBus485_Master_RxCounter; i++)
		{
			gBus485_Master_RxBuffer[i] = gUSART2_DMARx_Master_buffer[i];
		}

		//重新配置DMA
		LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_3,RS485_MASTER_DMARX_NUM);
		LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_3);
	}

	LL_USART_ClearFlag_IDLE(USART2);  //清除中断标志位
  /* USER CODE END USART2_IRQn 1 */
}

/**
  * @brief This function handles USART3 and USART4 interrupts.
  */
void USART3_4_IRQHandler(void)
{
  /* USER CODE BEGIN USART3_4_IRQn 0 */
  /* USER CODE END USART3_4_IRQn 0 */

  /* USER CODE BEGIN USART3_4_IRQn 1 */
	if(LL_USART_IsActiveFlag_IDLE(USART3))  //空闲
	{
		LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_4);  //关闭DMA

		gBus485_Slaver_RxCounter = RS485_SLAVER_DMARX_NUM-LL_DMA_GetDataLength(DMA1,LL_DMA_CHANNEL_4);  //

		for(uint16_t i = 0; i < gBus485_Slaver_RxCounter; i++)
		{
			gBus485_Slaver_RxBuffer[i] = gUSART3_DMARx_Slaver_buffer[i];
		}

		//重新配置DMA
		LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_4,RS485_SLAVER_DMARX_NUM);
		LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_4);
	}

	LL_USART_ClearFlag_IDLE(USART3);  //清除中断
  /* USER CODE END USART3_4_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
